package com.zxt.content.copywriting.events.upload;

import com.zxt.bean.ServiceException;
import com.zxt.bean.ResultCode;
import com.zxt.utils.ZXTCollectionsUtil;
import com.zxt.content.copywriting.manager.PublicCopywritingManager;
import com.zxt.content.copywriting.model.PrivateCopywriting;
import com.zxt.content.copywriting.model.PublicCopywriting;
import com.zxt.content.copywriting.utils.CheckDuplicates;
import com.zxt.content.user.manager.BUserManager;
import com.zxt.content.user.model.BUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 上传文案到公共文案 校验公共文案完整性
 *
 * @author zxt
 */
@Component
@Slf4j
@Order(2) //设置执行顺序的排序
public class PublicCopywritingCheckListener implements ApplicationListener<UploadCopywritingEvent> {

    private final PublicCopywritingManager publicCopywritingManager;
    private final BUserManager bUserManager;

    public PublicCopywritingCheckListener(PublicCopywritingManager publicCopywritingManager, BUserManager bUserManager) {
        this.publicCopywritingManager = publicCopywritingManager;
        this.bUserManager = bUserManager;
    }

    @Override
    public void onApplicationEvent(UploadCopywritingEvent uploadCopywritingEvent) {
        log.info("上传文案到公共文案--校验公共文案完整性");
        UploadCopywritingEventContext context = uploadCopywritingEvent.getContext();
        List<PrivateCopywriting> privateCopywritings = context.getPrivateCopywritings();

        List<String> themes = Optional.ofNullable(privateCopywritings).orElse(new ArrayList<>()).stream().map(PrivateCopywriting::getTheme).collect(Collectors.toList());

        //获取文案主题一致的公共文案
        List<PublicCopywriting> publicCopywritings = publicCopywritingManager.listByThemes(themes, context.getUserInfoVO().getId());
        Map<String, List<PublicCopywriting>> publicCopywritingGroupMap = Optional.ofNullable(publicCopywritings).orElse(new ArrayList<>()).stream().collect(Collectors.groupingBy(PublicCopywriting::getTheme));

        Map<Long, BUser> userMap = new HashMap<>();
        if (ZXTCollectionsUtil.isNotEmpty(publicCopywritings)) {
            List<Long> userIds = publicCopywritings.stream().map(PublicCopywriting::getUserId).collect(Collectors.toList());
            List<BUser> users = bUserManager.listByIds(userIds);
            userMap = Optional.ofNullable(users).orElse(new ArrayList<>()).stream().collect(Collectors.toMap(BUser::getId, Function.identity()));
        }


        for (PrivateCopywriting privateCopywriting : privateCopywritings) {
            List<PublicCopywriting> publicCopywritingList = publicCopywritingGroupMap.get(privateCopywriting.getTheme());

            if (ZXTCollectionsUtil.isNotEmpty(publicCopywritingList)) {
                for (PublicCopywriting publicCopywriting : publicCopywritingList) {
                    Float similarityRatio = CheckDuplicates.getSimilarityRatio(publicCopywriting.getContent(), privateCopywriting.getContent());
                    if (similarityRatio * 100 > 80) {
                        BUser bUser = userMap.get(publicCopywriting.getUserId());
                        throw new ServiceException(ResultCode.COMMON_MESSAGE, "文案: " + privateCopywriting.getTheme() + " 与上传者:" + bUser.getNickName() + " 上传的同标题文案重复率高达80%");
                    }
                }
            }
        }
    }
}
